package com.game;

import java.nio.channels.SocketChannel;

import org.apache.log4j.Logger;

import com.common.game.Player;
import com.common.utilities.ReceiveData;

/**
 * User: wattsc5
 * <p/>
 * Date: 2/26/12
 */
public class ServerGameAPI
{
	private static final Logger logger = Logger.getLogger(ServerGameAPI.class);

	private ServerGame game;

	/**
	 * Initializes a new instance of a ServerGameAPI.
	 *
	 * @param game the game in which to manipulate.
	 */
	public ServerGameAPI(ServerGame game)
	{
		this.game = game;
	}

	public void join(final ReceiveData arguments)
	{
		this.game.join(arguments.getSocketChannel(), arguments.getArgument(1));
	}

	public void bid(final ReceiveData arguments)
	{
		this.game.bid(arguments.getSocketChannel(), arguments.getArgumentAsInt(1), arguments.getArgumentAsInt(2));
	}

	public void challenge(final ReceiveData arguments)
	{
		this.game.challenge(arguments.getSocketChannel());
	}

	public void quit(final ReceiveData arguments)
	{
		Player player = this.game.getPlayerByName(arguments.getArgument(1));
		this.playerDisconnected(player.getSocketChannel());
		// TODO: Actually close the socket channel.
	}

	public void state(final ReceiveData arguments)
	{
		this.game.sendStateToPlayer(arguments.getArgumentAsInt(1));
	}

	public void playerDisconnected(SocketChannel socketChannel)
	{
		this.game.playerQuit(socketChannel);
	}
}
